import Vue from 'vue';
import VueRouter from 'vue-router';
// 路由组件
import Home from '@/views/Home.vue';

const originalPush = VueRouter.prototype.push
VueRouter.prototype.push = function push(location) {
  return originalPush.call(this, location).catch(err => err)
}

Vue.use(VueRouter);

// 默认(无权限)路由表
export let initRoutes = [
  {
    path: '/',
    redirect: '/home'
  },
  {
    path: '/home',
    name: 'home',
    component: Home
  },
  {
    path: '/about',
    name: 'about',
    component: function() {
      return import('../views/About.vue');
    }
  },
  {
    path: '/login',
    name: 'login',
    component: function() {
      return import('../views/login.vue');
    }
  }
]; 

let router = new VueRouter({
  routes: initRoutes
});

// 退出登录 需要初始化路由表和匹配规则
export function resetRouter() {
  let newRouter = new VueRouter({
    routes: initRoutes
  });

  router.matcher = newRouter.matcher;
}

export default router;